<template>
  <div class="scene-node-continue-judge">
    <el-form label-position="top" :rules="rules" label-width="80px" :model="formData" ref='nodeForm'>
      <el-divider></el-divider>
      <div class="judge-title">
      </div>
      <el-checkbox v-model="formData.isContinuousJudge" :disabled='disabled' class="judge-checkbox" @change="changeContinuous">场景持续时间检测</el-checkbox>
      <div v-if="formData.isContinuousJudge" class="long-time-scenes">
        <span>以上条件在</span>
        <el-form-item prop="duration">
          <el-input-number v-model="formData.duration" :controls='false' class="input-number" :min='1' :max='99'></el-input-number>
        </el-form-item>
        <span>秒内</span>
        <el-form-item prop="keepStrategy" class="select-type">
          <el-select v-model="formData.keepStrategy" :class="{'keep-strategy': formData.keepStrategy === 'part'}" @change="changeStrategy">
            <el-option label="持续满足" value="all"></el-option>
            <el-option label="至少满足" value="part"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.keepStrategy === 'part'" prop="keepTimes" class="fill-item">
          <el-input-number v-model="formData.keepTimes" :controls='false' class="input-number fill-text" :min='1' :max='99'></el-input-number>
          <span style="color:#A3A3A3">次</span>
        </el-form-item>
        <span>时成立</span>
      </div>
      <el-divider class="bottom-divider"></el-divider>
      <el-checkbox class="loop-check" v-model="formData.loopJudgment">场景循环判断：当场景不成立时，会循环判断。即产品执行逻辑会一直停留在该节点。直到<br>判断条件满足为止。</el-checkbox>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'ContinueJudge',
  props: {
    subScenes: {
      type: Array,
      default: () => []
    },
    form: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      disabled: false, // 禁止持续判断
      formData: {
        isContinuousJudge: false,
        duration: '',
        keepStrategy: '',
        keepTimes: '',
        loopJudgment: false
      },
      rules: {
        duration: [{ required: true, message: '请输入时间', trigger: 'blur' }],
        keepStrategy: [{ required: true, message: '请选择类型', trigger: 'change' }],
        keepTimes: [{ required: true, message: '请输入次数', trigger: 'blur' }]
      }
    }
  },
  mounted() {
  },
  methods: {
    changeContinuous(val) {
      if (!val) {
        this.formData.duration = undefined
        this.formData.keepStrategy = ''
      }
    },
    changeStrategy(val) {
      this.formData.keepTimes = val === 'part' ? undefined : null
    },
    saveChange() {
      this.$emit('update:form', Object.assign(this.form, this.formData))
    }
  },
  watch: {
    subScenes: {
      handler(val) {
        if (val) {
          this.formData = {
            isContinuousJudge: this.form.isContinuousJudge,
            duration: this.form.duration,
            keepStrategy: this.form.keepStrategy,
            keepTimes: this.form.keepTimes,
            loopJudgment: this.form.loopJudgment
          }
          this.disabled = false
          this.subScenes.forEach(scene => {
            scene.signals.forEach(signal => {
              if (signal.signalGroup && signal.signalGroup !== 'vehicleSignal') {
                this.disabled = true
                this.formData.isContinuousJudge = false
                this.changeContinuous()
              }
            })
          })
        }
      },
      deep: true
    },
    'formData.isContinuousJudge': { handler() { this.saveChange() } },
    'formData.duration': { handler() { this.saveChange() } },
    'formData.keepStrategy': { handler() { this.saveChange() } },
    'formData.keepTimes': { handler() { this.saveChange() } },
    'formData.loopJudgment': { handler() { this.saveChange() } }
  }
}
</script>

<style lang="scss">

.scene-node-continue-judge {
  .judge-title{
    margin-bottom: 15px;
  }
  .long-time-title{
    margin-left: 10px;
    color: #C0C4CC;
  }
  .judge-checkbox{
    margin-bottom: 10px;
  }
  .long-time-scenes{
    display: flex;
    .input-number{
      width: 60px;
      margin: 0 10px;
      text-align: left;
      .el-input__inner{
        text-align: left;
      }
    }
    .fill-item{
      position: relative;
      .fill-text{
        margin-left: -10px;
      }
      span{
        position: absolute;
        top: 0px;
        left: 30px;
      }
    }
    .select-type{
      margin: 0 10px;
      width: 100px;
      .keep-strategy{
        .el-input, .el-input.is-focus{
          .el-input__inner, .el-input__inner:hover, .el-input__inner:focus{
            border-right: none;
            transition: none;
            border-color: #e0e0e0;
          }
        }
      }
      &.is-error{
        .keep-strategy .el-input__inner{
          border-right: 1px solid #E15863;
          border-color: #E15863;
        }
      }
    }
    span{
      line-height: 32px;
    }
  }
  .bottom-divider{
    margin-top: 8px;
  }
  .loop-check{
    .el-checkbox__input{
      transform: translateY(-19px);
    }
  }
}
</style>
